home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / perlnt.zip / eg / h2ph.cmd < prev    next >
OS/2 REXX Batch file  |  1993-07-25  |  5KB  |  217 lines

  1. @rem = '-*- Perl -*-';
  2. @rem = '
  3. @echo off
  4. perl -S %0.cmd %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. ';
  7.  
  8. #$perlincl = 'c:/win32app/ingr/perl/inc';
  9. $perlincl = 'e:/perl-4.036/inc';
  10.  
  11. $incdir = 'd:/mstools/h';
  12.  
  13. @isatype = split(' ',<<END);
  14.     char    uchar    u_char
  15.     short    ushort    u_short
  16.     int    uint    u_int
  17.     long    ulong    u_long
  18.     FILE
  19. END
  20.  
  21. @isatype{@isatype} = (1) x @isatype;
  22.  
  23. #@ARGV = ('-') unless @ARGV;
  24.  
  25. opendir(D, $incdir) || die "can'd open $incdir directory";
  26. @dirs = readdir (D);
  27. closedir D;
  28. @ARGV = grep (/^.*\.h$/i, @dirs);
  29.  
  30. foreach $file (@ARGV) {
  31.     if ($file eq '-') {
  32.     open(IN, "-");
  33.     open(OUT, ">-");
  34.     }
  35.     else {
  36.     ($outfile = $file) =~ s/\.h$/.ph/i || next;
  37.     print "$file -> $outfile\n";
  38.     if ($file =~ m|^(.*)/|) {
  39.         $dir = $1;
  40.         if (!-d "$perlincl/$dir") {
  41.         mkdir("$perlincl/$dir",0777);
  42.         }
  43.     }
  44.     open(IN,"$incdir/$file") 
  45.         || ((warn "Can't open $incdir/$file: $!\n"),next);
  46.     open(OUT,">$perlincl/$outfile") 
  47.         || die "Can't create $outfile: $!\n";
  48.     }
  49.     while (<IN>) {
  50.     chop;
  51.     while (/\\$/) {
  52.         chop;
  53.         $_ .= <IN>;
  54.         chop;
  55.     }
  56.     if (s:/\*:\200:g) {
  57.         s:\*/:\201:g;
  58.         s/\200[^\201]*\201//g;    # delete single line comments
  59.         if (s/\200.*//) {        # begin multi-line comment?
  60.         $_ .= '/*';
  61.         $_ .= <IN>;
  62.         redo;
  63.         }
  64.     }
  65.     if (s/^#\s*//) {
  66.         if (s/^define\s+(\w+)//) {
  67.         $name = $1;
  68.         $new = '';
  69.         s/\s+$//;
  70.         if (s/^\(([\w,\s]*)\)//) {
  71.             $args = $1;
  72.             if ($args ne '') {
  73.             foreach $arg (split(/,\s*/,$args)) {
  74.                 $arg =~ s/^\s*([^\s].*[^\s])\s*$/$1/;
  75.                 $curargs{$arg} = 1;
  76.             }
  77.             $args =~ s/\b(\w)/\$$1/g;
  78.             $args = "local($args) = \@_;\n$t    ";
  79.             }
  80.             s/^\s+//;
  81.             do expr();
  82.             $new =~ s/(["\\])/\\$1/g;
  83.             if ($t ne '') {
  84.             $new =~ s/(['\\])/\\$1/g;
  85.             print OUT $t,
  86.               "eval 'sub $name {\n$t    ${args}eval \"$new\";\n$t}';\n";
  87.             }
  88.             else {
  89.             print OUT "sub $name {\n    ${args}eval \"$new\";\n}\n";
  90.             }
  91.             %curargs = ();
  92.         }
  93.         else {
  94.             s/^\s+//;
  95.             do expr();
  96.             $new = 1 if $new eq '';
  97.             if ($t ne '') {
  98.             $new =~ s/(['\\])/\\$1/g;
  99.             print OUT $t,"eval 'sub $name {",$new,";}';\n";
  100.             }
  101.             else {
  102.             print OUT $t,"sub $name {",$new,";}\n";
  103.             }
  104.         }
  105.         }
  106.         elsif (/^include\s+<(.*)>/) {
  107.         ($incl = $1) =~ s/\.h$/.ph/;
  108.         print OUT $t,"require '$incl';\n";
  109.         }
  110.         elsif (/^ifdef\s+(\w+)/) {
  111.         print OUT $t,"if (defined &$1) {\n";
  112.         $tab += 4;
  113.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  114.         }
  115.         elsif (/^ifndef\s+(\w+)/) {
  116.         print OUT $t,"if (!defined &$1) {\n";
  117.         $tab += 4;
  118.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  119.         }
  120.         elsif (s/^if\s+//) {
  121.         $new = '';
  122.         do expr();
  123.         print OUT $t,"if ($new) {\n";
  124.         $tab += 4;
  125.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  126.         }
  127.         elsif (s/^elif\s+//) {
  128.         $new = '';
  129.         do expr();
  130.         $tab -= 4;
  131.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  132.         print OUT $t,"}\n${t}elsif ($new) {\n";
  133.         $tab += 4;
  134.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  135.         }
  136.         elsif (/^else/) {
  137.         $tab -= 4;
  138.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  139.         print OUT $t,"}\n${t}else {\n";
  140.         $tab += 4;
  141.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  142.         }
  143.         elsif (/^endif/) {
  144.         $tab -= 4;
  145.         $t = "\t" x ($tab / 8) . ' ' x ($tab % 8);
  146.         print OUT $t,"}\n";
  147.         }
  148.     }
  149.     }
  150.     print OUT "1;\n";
  151. }
  152.  
  153. sub expr {
  154.     while ($_ ne '') {
  155.     s/^(\s+)//        && do {$new .= ' '; next;};
  156.     s/^(0x[0-9a-fA-F]+)//    && do {$new .= $1; next;};
  157.     s/^(\d+)//        && do {$new .= $1; next;};
  158.     s/^("(\\"|[^"])*")//    && do {$new .= $1; next;};
  159.     s/^'((\\"|[^"])*)'//    && do {
  160.         if ($curargs{$1}) {
  161.         $new .= "ord('\$$1')";
  162.         }
  163.         else {
  164.         $new .= "ord('$1')";
  165.         }
  166.         next;
  167.     };
  168.     s/^sizeof\s*\(([^)]+)\)/{$1}/ && do {
  169.         $new .= '$sizeof';
  170.         next;
  171.     };
  172.     s/^([_a-zA-Z]\w*)//    && do {
  173.         $id = $1;
  174.         if ($id eq 'struct') {
  175.         s/^\s+(\w+)//;
  176.         $id .= ' ' . $1;
  177.         $isatype{$id} = 1;
  178.         }
  179.         elsif ($id eq 'unsigned') {
  180.         s/^\s+(\w+)//;
  181.         $id .= ' ' . $1;
  182.         $isatype{$id} = 1;
  183.         }
  184.         if ($curargs{$id}) {
  185.         $new .= '$' . $id;
  186.         }
  187.         elsif ($id eq 'defined') {
  188.         $new .= 'defined';
  189.         }
  190.         elsif (/^\(/) {
  191.         s/^\((\w),/("$1",/ if $id =~ /^_IO[WR]*$/i;    # cheat
  192.         $new .= " &$id";
  193.         }
  194.         elsif ($isatype{$id}) {
  195.         if ($new =~ /{\s*$/) {
  196.             $new .= "'$id'";
  197.         }
  198.         elsif ($new =~ /\(\s*$/ && /^[\s*]*\)/) {
  199.             $new =~ s/\(\s*$//;
  200.             s/^[\s*]*\)//;
  201.         }
  202.         else {
  203.             $new .= $id;
  204.         }
  205.         }
  206.         else {
  207.         $new .= ' &' . $id;
  208.         }
  209.         next;
  210.     };
  211.     s/^(.)//            && do {$new .= $1; next;};
  212.     }
  213. }
  214. __END__
  215. :endofperl
  216.  
  217.